package com.mathlearning.auth.repository;

import com.mathlearning.auth.entity.Comment;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CommentRepository extends JpaRepository<Comment, Long> {
    
    // 获取顶级评论（按最热排序）
    @Query("SELECT c FROM Comment c WHERE c.discussionId = :discussionId " +
           "AND c.parentId IS NULL AND c.isDeleted = 0 ORDER BY c.likes DESC")
    Page<Comment> findTopCommentsByHot(Long discussionId, Pageable pageable);
    
    // 获取顶级评论（按最新排序）
    @Query("SELECT c FROM Comment c WHERE c.discussionId = :discussionId " +
           "AND c.parentId IS NULL AND c.isDeleted = 0 ORDER BY c.createdAt DESC")
    Page<Comment> findTopCommentsByLatest(Long discussionId, Pageable pageable);
    
    // 获取评论的回复列表
    Page<Comment> findByParentIdAndIsDeleted(Long parentId, Integer isDeleted, Pageable pageable);
    
    // 统计讨论的评论总数
    long countByDiscussionIdAndIsDeleted(Long discussionId, Integer isDeleted);
    
    // 获取用户的所有评论（包括回复）
    List<Comment> findByAuthorIdAndIsDeleted(Long authorId, Integer isDeleted);
    
    // 删除讨论的所有评论
    void deleteByDiscussionId(Long discussionId);
}








